﻿using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KZ.Express.H.Presenter;
using KZ.Express.H.ENT;

namespace KZ.Express.H.Orders
{
    public partial class CustomerExportView : BaseView
    {
        #region Private Variables

        #endregion Private Variables

        public CustomerExportView()
        {
            InitializeComponent();


            this.TopLevel = true;
            _presenter = new CustomerExportPresenter(this);
        }

        private void CustomerExportView_Load(object sender, EventArgs e)
        {
            //LoadCarrier();
            LoadCustomer();
        }

        //private void LoadCarrier()
        //{
        //    try
        //    {
        //        cmbCarrierID.ValueMember = "CarrierID";
        //        cmbCarrierID.DisplayMember = "CarrierName";
        //        DataTable dt = ((OriginalPriceDetailPresenter)_presenter).LoadDataTable<CarrierENT>(null);
        //        DataRow row = dt.NewRow();
        //        dt.Rows.InsertAt(row, 0);
        //        cmbCarrierID.DataSource = dt;
        //    }
        //    catch (BusinessLogicException ex)
        //    {
        //        SetMessage(ex.Message);
        //    }
        //}

        private void LoadCustomer()
        {
            try
            {
                cmbCustomerID.ValueMember = "CustomerID";
                cmbCustomerID.DisplayMember = "CustomerName";
                DataTable dt = _presenter.LoadDataTable<CustomerENT>(null);
                DataRow row = dt.NewRow();
                dt.Rows.InsertAt(row, 0);
                cmbCustomerID.DataSource = dt;
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    Cursor = Cursors.WaitCursor;

                    string fileName = sfd.FileName;
                    string template = Path.Combine(Application.StartupPath, "Template\\Debit noted.xlt");
                    DataRowView customerRow = cmbCustomerID.SelectedItem as DataRowView;

                    if (customerRow == null) throw new BusinessLogicException("Bạn phải chọn khách hàng");
                    //DataTable dt = new DataTable();
                    //int rateCount = 0;
                    DataTable dt = ((CustomerExportPresenter)_presenter).LoadOrder(dtpFrom.Value, dtpTo.Value, Utility.IntParse(customerRow["CustomerID"]));
                    if (dt == null || dt.Rows.Count == 0) throw new BusinessLogicException("Không có dữ liệu");

                    EnumerableRowCollection<DataRow> rows = dt.AsEnumerable();
                    int rateCount;
                    if ((rateCount = rows.GroupBy(r => r.Field<decimal?>("RATE")).Count()) > 1)
                    {
                        DialogResult result = MessageBox.Show("Dữ liệu về tỉ giá không đồng bộ trong khoảng thời gian đã chọn. Bạn có muốn tiếp tục export dữ liệu", String.Empty, MessageBoxButtons.YesNo);
                        if (result != DialogResult.Yes)
                            return;
                    }

                    ((CustomerExportPresenter)_presenter).Export(fileName,  customerRow, dt, rateCount, template,txtExchangeRate.Text);
                    DialogResult = DialogResult.Yes;
                }
            }
            catch (BusinessLogicException ex)
            {
                base.SetMessage(ex.Message);
            }
        }

        public DateTime From
        {
            set { dtpFrom.Value = value; }
        }

        public DateTime To
        {
            set { dtpTo.Value = value; }
        }
    }
}
